﻿
using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using WebApplication1.API返回类;
using WebApplication1.Models.商品信息.企业;
using WebApplication1.Models.商品信息.养殖;

namespace WebApplication1.Controllers
{
    /// <summary>
    /// 养殖企业控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class EnterpriseController : ControllerBase
    {
        private readonly ISqlSugarClient _db;
        private readonly IMapper mapper;

        public EnterpriseController(SqlSugar.ISqlSugarClient db,IMapper mapper)
        {
            this._db = db;
            this.mapper = mapper;
        }

        /// <summary>
        /// 创建养殖企业信息
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        [HttpPost]
        public APIResult<int> CreateEnterprise(EnterpriseModel e)
        {
            int r = _db.Insertable(e).ExecuteReturnIdentity();
            if (r > 0)
            {
                return APIResult<int>.Success("创建养殖企业成功", r);
            }
            else
            {
                return APIResult<int>.Fail("创建养殖企业失败");
            }
        }

        /// <summary>
        /// 查询养殖企业信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public APIResult<List<EnterpriseModel>> GetEnterpriseAll(int id)
        {
            var list = _db.Queryable<EnterpriseModel>().Where(x => x.RId == id).OrderByDescending(x => x.EnterpriseId);

            return new APIResult<List<EnterpriseModel>>()
            {
                Code = APIEnum.成功,
                Message = "查询养殖企业信息成功",
                Data = list.ToList(),
            };
        }

        /// <summary>
        /// 查询动检证信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public APIResult<List<QuarantineModel>> GetQuarantineAll(int id)
        {
            var list = _db.Queryable<QuarantineModel>().Where(x => x.RId == id);

            return new APIResult<List<QuarantineModel>>()
            {
                Code = APIEnum.成功,
                Message = "查询养殖企业信息成功",
                Data = list.ToList(),
            };
        }

        /// <summary>
        /// 创建动检证信息
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        [HttpPost]
        public APIResult<int> CreateQuarantine(QuarantineModel e)
        {
            int r = _db.Insertable(e).ExecuteReturnIdentity();
            if (r > 0)
            {
                return APIResult<int>.Success("创建动检证成功", r);
            }
            else
            {
                return APIResult<int>.Fail("创建动检证失败");
            }
        }

        /// <summary>
        /// 查询养殖企业图片信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public APIResult<List<EnterprisePicModel>> GetEnterprisepicAll(int id)
        {
            var list = _db.Queryable<EnterprisePicModel>().Where(x => x.RId == id);

            return new APIResult<List<EnterprisePicModel>>()
            {
                Code = APIEnum.成功,
                Message = "查询养殖企业信息成功",
                Data = list.ToList(),
            };
        }

        /// <summary>
        /// 创建养殖企业图片信息
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        [HttpPost]
        public APIResult<int> CreateEnterprisepic(EnterprisePicModel e)
        {
            int r = _db.Insertable(e).ExecuteReturnIdentity();
            if (r > 0)
            {
                return APIResult<int>.Success("创建养殖企业图片成功", r);
            }
            else
            {
                return APIResult<int>.Fail("创建养殖企业图片失败");
            }
        }
    }
}
